package com.androidbook.bro;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends MultiTouchActivity {

	int k = 0;
	static String line ;
	static String signal;
	

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start);
		ToWrite = (TextView) findViewById(R.id.MyText);
		BtnRight = (Button) findViewById(R.id.btnRight);
		BtnLeft = (Button) findViewById(R.id.btnLeft);
		BtnUp = (Button) findViewById(R.id.btnUp);
		BtnDown = (Button) findViewById(R.id.btnDown);

		BtnLeft.setOnTouchListener(this);
		BtnRight.setOnTouchListener(this);
		BtnUp.setOnTouchListener(this);
		BtnDown.setOnTouchListener(this);

		// PressChange();
		StartConnect thread = new StartConnect();
		thread.start();

	}

	

	public class StartConnect extends Thread {
		double[] str = { 1, 0, 0, 1, 0, 0 };

		public StartConnect() {PressButton = str;}
		private static final int port = 8198;
		private static final String host = "192.168.99.42";
						
		public void run() {
			//double[] str = {1,0,0,1,0,0};
			line = "";
			try {
				Socket socket = new Socket(host, port);
				BufferedReader bis = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
				PrintWriter bos = new PrintWriter(socket.getOutputStream(), true);
				signal = "continue";
				while(true){
					if (BtnLeft.isPressed())
						PressButton[1] = 1;
					else
						PressButton[1] = 0;

					if (BtnRight.isPressed())
						PressButton[2] = 1;
					else
						PressButton[2] = 0;
					if (BtnUp.isPressed())
						PressButton[3] = 1;
					else
						PressButton[3] = 0;
					if (BtnDown.isPressed())
						PressButton[4] = 1;
					else
						PressButton[4] = 0;
					if (PressButton[0] == 1) {
						for (int j = 0; j < 6; ++j) {
							line = line + " " + String.valueOf(PressButton[j]);
						}
						
						System.out.println("Sending package");
						bos.println(line);
						Thread.sleep(1);
						bos.flush();
						line = "";
					}
				}

			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
